/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.inventario.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;
import com.isd.global.dao.BaseDAO;
import com.isd.inventario.modelo.Marca;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author ctrosch
 */
@Stateless
@LocalBean
public class RubroClienteDAO extends BaseDAO {
    
    public List<String> getTipoProductoByNrocta(Map<String,String> filtro){

        try {
            //Filtro adicional
            filtro.put("tipo ", "IS NOT NULL");
            
            String sQuery = "SELECT DISTINCT i.tipo FROM RubrosCliente i ";
            sQuery += generarStringFiltro(filtro,"i", true);
 
            //System.err.println("getTipoProductoByNrocta: " + sQuery);
            
            Query q = getEntityManager().createQuery(sQuery);            
            return q.getResultList();

        }catch(Exception e){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getTipoProductoByNrocta", e);            
            return new ArrayList<String>();
        }
    }
    
//    public List<TipoProducto> getTipoProductoByNrocta(Map<String,String> filtro){
//
//        try {
//            String subQuery = "SELECT DISTINCT i.tippro FROM RubrosCliente i ";
//            subQuery += generarStringFiltro(filtro,"i", true);
//            String sQuery = "SELECT t FROM TipoProducto t WHERE t.tippro IN ("+subQuery+")";
//            sQuery += " ORDER BY t.descrp ASC";
//            
//            Query q = getEntityManager().createQuery(sQuery);
//
//            return q.getResultList();
//
//        }catch(Exception e){
//            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getTipoProductoByNrocta", e);            
//            return new ArrayList<TipoProducto>();
//        }
//    }

    public List<String> getSubtipoByNrocta(Map<String,String> filtro){

        try {
            //Filtro adicional
            filtro.put("subtipo ", "IS NOT NULL");
            
            String sQuery = "SELECT DISTINCT i.subtipo FROM RubrosCliente i ";
            sQuery += generarStringFiltro(filtro,"i", true);
 
            //System.err.println("getSubtipoByNrocta: " + sQuery);
            
            Query q = getEntityManager().createQuery(sQuery);
            
            return q.getResultList();
            
            /**
            String subQuery = "SELECT DISTINCT i.rubr01 FROM RubrosCliente i ";
            subQuery += generarStringFiltro(filtro,"i", true);

            String sQuery = "SELECT t FROM SubTipo t WHERE t.id.tippro= "+ filtro.get("tippro = ")
                    + " AND t.valrub IN ("+subQuery+")";
            sQuery += " ORDER BY t.descrp ASC";

            Query q = getEntityManager().createQuery(sQuery);
            return q.getResultList(); 
            * */

        }catch(Exception e){            
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getSubtipoByNrocta", e);            
            return new ArrayList<String>();
        }
    }

    /**
     * Devuelve la lista de todas las marcas disponible que tenga el cliente en el catalogo
     *
     * @param filtro
     * @param esVendedor
     * @return
     */

    public List<Marca> getMarcaByTipoProductoNroCta(Map<String,String> filtro){

        try {
            String subQuery = "SELECT DISTINCT i.rubr02 FROM RubrosCliente i ";
            subQuery += generarStringFiltro(filtro,"i", true);
 
            String sQuery = "SELECT t FROM Marca t WHERE t.id.tippro= " + filtro.get("tippro = ")
                    + " AND t.valrub IN ("+subQuery+")";
            sQuery += " ORDER BY t.descrp ASC";

            Query q = getEntityManager().createQuery(sQuery);
            
            return q.getResultList();

        }catch(Exception e){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getMarcaByTipoProductoNroCta", e);
            return new ArrayList<Marca>();
        }
    }
    
    
    public List<String> getMarcaByNroCta(Map<String,String> filtro){

        try {
            //Filtro adicional
            filtro.put("marca ", "IS NOT NULL");
            
            String sQuery = "SELECT DISTINCT i.marca FROM RubrosCliente i ";
            sQuery += generarStringFiltro(filtro,"i", true);
 
            Query q = getEntityManager().createQuery(sQuery);
            
            return q.getResultList();

        }catch(Exception e){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getMarcaByNrocta", e);
            return new ArrayList<String>();
        }
    }
    
    
    
    

    public List<String> getOrigenByNrocta(Map<String, String> filtro) {
        try {

            String sQuery = "SELECT DISTINCT i.origen FROM RubrosCliente i ";
            sQuery += generarStringFiltro(filtro,"i", true);
            Query q = getEntityManager().createQuery(sQuery);
            return q.getResultList();

        }catch(Exception e){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getOrigenByNrocta", e);
            return new ArrayList<String>();
        }
    }
    
    public List<String> getNovedadByNrocta(Map<String, String> filtro) {
        try {

            String sQuery = "SELECT DISTINCT i.novedad FROM RubrosCliente i ";
            sQuery += generarStringFiltro(filtro,"i", true);

            Query q = getEntityManager().createQuery(sQuery);
            return q.getResultList();

        }catch(Exception e){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getNovedadByNrocta", e);
            return new ArrayList<String>();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
